/* 
* Class name: Options *
* 
* Version info: v4.0 *
* 
* Description: The Options menu is shown and the operations about them is done here.
* 
* Copyright by Group4 from CmpE450 course *
*/ 

package com.GameSpace.SameGame;

import java.util.ArrayList;

import com.GameSpace.CoreAssets.Option;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.GameSpace.SameGame.R;
import com.GameSpace.CoreAssets.SoundManager;

public class Options extends ListActivity {
	
    private ArrayList<Option> m_orders = null;
    private OptionAdapter m_adapter;
    private Runnable viewOrders;
    
    /* Called when the activity is first created. */
    @Override
    public void onCreate( Bundle savedInstanceState ){
    	super.onCreate(savedInstanceState);
        
        setContentView(R.layout.settings);
        //initialize option array
        m_orders = new ArrayList<Option>();
        
        this.m_adapter = new OptionAdapter(this, R.layout.inttypeoption, m_orders);
        setListAdapter(this.m_adapter);
       
        viewOrders = new Runnable(){
            @Override
            public void run() {
                getOrders();
            }
        };
        
        Thread thread =  new Thread(null, viewOrders);
        thread.start();
        
        ImageView home = (ImageView)findViewById(R.id.goHome1);        
    	home.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	Intent GotoMenuIntent = new Intent(Options.this, GameSpace.class);
        		startActivity(GotoMenuIntent);
               
            }
        });
    	ImageView Logo;
        Logo  = (ImageView)findViewById(R.id.ImgLogo1);
        //click event handler for other company logo button
        Logo.setOnClickListener(new OnClickListener() {	        	
        	public void onClick(View v) {
        		//click the class of the screen which shows other games of the company
        		Intent GameAdsIntent= new Intent(Options.this,GameAds.class);
        		startActivity(GameAdsIntent);
        	}
        });
    }
    
    
    private Runnable returnRes = new Runnable() {

        @Override
        public void run() {
            if(m_orders != null && m_orders.size() > 0){
                m_adapter.notifyDataSetChanged();
                for(int i=0;i<m_orders.size();i++)
                	m_adapter.add(m_orders.get(i));
            }
            m_adapter.notifyDataSetChanged();
        }
    };
    
    //returns options of the user
    private void getOrders(){
    	
        Option option = new Option();
        option.setType("boolean");
        option.setLabel(" Sound     ");
        option.setValue("ON");
        m_orders = new ArrayList<Option>();
        m_orders.add(option);  
        option = new Option();
        option.setType("boolean");
        option.setLabel(" Vibration");
        option.setValue("OFF");
        m_orders.add(option);   
   
        runOnUiThread(returnRes);
    }
    

    //option adapter to retrieve and save the state of the options
    private class OptionAdapter extends ArrayAdapter<Option> implements OnClickListener{

        private ArrayList<Option> items;

        public OptionAdapter( Context context, int textViewResourceId, ArrayList<Option> items ){
                super(context, textViewResourceId, items);
                this.items = items;
        }
        
        //an overriden method to get the options according to position as a view
        @Override
        public View getView( int position, View convertView, ViewGroup parent ){
        	
            View v = convertView;
            TextView tt;  
            TextView bt;
            CheckBox checkBox;
            CheckBox checkBox2;
            
            Option o = items.get(position);
            if (o != null) {
            	LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            	if(o.getType().equalsIgnoreCase("integer")){
                		
                    v = vi.inflate(R.layout.inttypeoption, null);
            		tt = (TextView) v.findViewById(R.id.TextView01);
                    bt = (TextView) v.findViewById(R.id.TextView02);
                    if (tt != null) {
                        tt.setText(o.getLabel());                            }
                    if(bt != null){
                        bt.setText(o.getValue());
                    }
                }
                else{
                    v = vi.inflate(R.layout.booltypeoption, null);
                    if(o.getLabel().equalsIgnoreCase(" Sound     ")){
	                    tt = (TextView) v.findViewById(R.id.text1);
	                    checkBox = (CheckBox)v.findViewById(R.id.CheckBox01);
	                    checkBox.setChecked(SoundManager.isSoundsEnabled());
	                                             
	                    if (tt != null) {
	                        tt.setText(o.getLabel());
	                    }
	                    if(checkBox!=null){
	                        checkBox.setInputType(1);
	                        checkBox.setOnClickListener(this);
	                    }
                    }
                    else{
                        tt = (TextView) v.findViewById(R.id.text1);
	                    checkBox2 = (CheckBox)v.findViewById(R.id.CheckBox01);
	                    checkBox2.setChecked(VibrationManager.getVibrationEnabled());
	                       
	                    if (tt != null) {
	                        tt.setText(o.getLabel());
	                    }
	                    if(checkBox2!=null){
	                        checkBox2.setInputType(2);
	                        checkBox2.setOnClickListener(this);
	                    }
                    }
                       

                }
            }
            return v;
        }
        
        
        //click event handler to check or uncheck an option of the user
    	public void onClick( View v ){
    		CheckBox checkBox = (CheckBox)v;
    		//if first check box is clicked set the sound option as active
    		if(checkBox.getInputType()==1)
    			SoundManager.setSoundsEnabled(checkBox.isChecked());
    		
    		//if second check box is clicked set the vibration option as active
    		else if(checkBox.getInputType()==2)
    			VibrationManager.setVibrationEnabled(checkBox.isChecked());
    	}
}


	
}